package cn.xiezonjie.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BaseDao {
         Connection conn = null;
		 Statement stmt = null;
		 PreparedStatement pstmt=null;
		 ResultSet rs=null;
		
		static 
		{
			try 
			{
				Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			} 
			catch (ClassNotFoundException e) {
				System.out.println(e.getMessage());
			}
		}
		
		/** ��ݿ����ӷ���**/
		public void getConnection() 
		{
			try 
			{
				String url = "jdbc:sqlserver://localhost:1433;DatabaseName=OrderSystem";
				String user = "sa";
				String pwd = "123";
				this.conn = DriverManager.getConnection(url, user, pwd); // ����Connection����con,olLearnΪSQLServer��ݿ����
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		public Connection getConn()
		{
			if(conn==null)this.getConnection();
			return conn;
		}
		
		
		/**
		 * �������ʽ����
		 * @return
		 */
		public Statement getStmt()
		{
			if(stmt!=null)return stmt;
			if(conn==null)this.getConnection();
			try 
			{
				this.stmt = this.conn.createStatement();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
			return stmt;
		}
		
		public void closeConn(){
			if(conn!=null)
				try 
				{
					conn.close();
				} 
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
		}
		
		public void closeStmt(){
			if(stmt!=null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		
		public void closeAll(){
			if(rs!=null)
				try {
					rs.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			this.closeStmt();
			this.closeConn();
			if(pstmt!=null)
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
}
